#!/bin/bash

## eg: sh serve.sh start

funcOne() {
    sh serve_one.sh "$1" ${service_name} ${service_type}
    return
}

funcAll() {
    echo "all $1"

    rpc_service_list=("user" "sstwords" "shorturl")

    echo "start rpc"

    for rpc_service_name in ${rpc_service_list[@]}; do
        # echo ${rpc_service_name}
        sh serve_one.sh "$1" ${rpc_service_name} rpc
    done

    echo "start api"

    api_service_list=("user" "sstwords" "search" "shorturl")
    for api_service_name in ${api_service_list[@]}; do
        # echo ${api_service_name}
        sh serve_one.sh "$1" ${api_service_name} api
    done

    echo "all required services have been $1, please wait a moment."
    return
}

base_path="/home/zhubing/web/golang/src/go-zero/book"

act_type=$1     # start stop restart
service_name=$2 # user sstwords search shorturl
service_type=$3 # api | rpc

if [ "${act_type}" == "start" ]; then
    if [ "${service_name}" != "" ]; then
        if [ "${service_type}" != "" ]; then
            funcOne start
        fi
    else
        funcAll start
    fi

elif [ "${act_type}" == "stop" ]; then
    if [ "${service_name}" != "" ]; then
        if [ "${service_type}" != "" ]; then
            funcOne stop
        fi
    else
        funcAll stop
    fi
elif [ "${act_type}" == "restart" ]; then
    if [ "${service_name}" != "" ]; then
        if [ "${service_type}" != "" ]; then
            funcOne restart
        fi
    else
        funcAll restart
    fi
else
    echo "Please make sure the position variable is start or stop."
fi
